<link rel="stylesheet" href="/human/css/human.css">

<div id="human-section-1" class="section-profile">
    <div class="row">
        <div class="topleft duk-icon"><img onclick="removeSection('human-section-1')" src="/gui/img/x.png"></div>
        <div class="column section-border" style="flex:25%;text-align:left;padding:15px;">
             <h1 style="font-size: 70px;margin-top:-20px;">Human</h1>
             <h2 style="margin-top:-55px;">emulate human behavior</h2>
             <p>
                 This plugin allows you to design a human that will emulate user behavior on an endpoint. Use the following instructions to build and deploy your own human.
             </p>
             <br/>
        </div>
        <div class="column" style="flex:75%;margin-right: -25px;align-items: stretch;display: flex;">
            <div class="row row-interior install-list">
                <div class="column column-interior install-container" style="text-align: left; flex:25%;">
                    <div class="background-text">1</div>
                    <span>Install Python3 on the target workstation</span>
                </div>
                <div class="column column-interior install-container" style="text-align: left; flex:25%;">
                    <div class="background-text">2</div>
                    <span>Install Python3 Virtualenv on the target workstation</span>
                </div>
                <div class="column column-interior install-container" style="text-align: left; flex:25%;">
                    <div class="background-text">3</div>
                    <span>Install Chrome on the target workstation</span>
                </div>
                <div class="column column-interior install-container" style="text-align: left; flex:25%;">
                    <div class="background-text">4</div>
                    <span>Build a human below and run the download cradle on the target workstation</span>
                </div>
            </div>
        </div>
    </div>
</div>
<div id="human-section-2" class="section-profile human-basic">
    <div class="row">
        <div class="topleft duk-icon"><img onclick="removeSection('human-section-2')" src="/gui/img/x.png"></div>
        <div class="topright duk-icon"></div>
        <div class="column section-border" style="flex:25%;text-align:left;padding:15px;">
             <h1 id="human-name-header" style="font-size:70px;margin-top:-20px;">Name...</h1>
                <h2 style="margin-top:-55px">build your human</h2>
             <p>
                Design and generate your human here, or select a pre-existing human to deploy. Use the download command to install and start your human.
             </p>
             <br>

        </div>
        <div class="column" style="flex:75%;text-align: left;margin-right: -25px;">
            <div class="row row-interior">
                <div id="behavior-options" class="column column-interior human-box">
                    <table>
                        <tr>
                            <td><p>Name:</p></td>
                            <td><input id="human-name" type="text" placeholder="Enter human's name..."></td>
                        </tr>
                        <tr>
                            <td><p>Platform:</p></td>
                            <td>
                                <select id="base-platform">
                                    <option disabled="disabled" selected="">Select target OS</option>
                                    <option value="darwin">MacOS</option>
                                    <option value="linux">Linux</option>
                                    <option value="windows-psh">Windows (PowerShell)</option>
                                </select>
                            </td>
                        </tr>
                    </table>
                    <hr>
                    <h4>Select your human's behaviors:</h4>
                    <ul id="human-tasks">
                        {% for w in workflows %}
                        <li>
                            <input type="checkbox" name="human-{{w.name}}" id="human-{{w.name}}"/>
                            <span>{{ w.description }}</span>
                        </li>
                        {% endfor %}
                    </ul>
                    <hr>
                    <h4>Custom Commands:</h4>
                        <button id="append-command" class="command-button atomic-button" style="width:30%;background-color:green">Add Command</button>
                        <button id="clear-commands" class="command-button atomic-button" style="width:30%;background-color:firebrick">Clear Commands</button>
                        <br><br>
                        <div id="input-command">
                            <!-- Place holder for appended custom commands -->
                        </div>
                    <hr>
                    <h4>Human behavior configuration:</h4>
                    <table>
                        <tr>
                            <td><p>Task Sleep Interval</p></td>
                            <td class="duk-icon duk-table-icon"><img id="duk-task-sleep-interval" src="/gui/img/duk.png"></td>
                            <td><div><input class="queueOption" type="range" min="5" max="50" value="10" class="slider" id="human-task-interval" name="human-task-interval" onchange="updateNumberField(this);"/></div></td>
                            <td><input type="number" id="human-task-interval-text" onchange="updateSlider(this);" onload="defaultNumberField(this);"></td>
                        </tr>
                        <tr>
                            <td><p>Task Cluster Sleep Interval</p></td>
                            <td class="duk-icon duk-table-icon"><img id="duk-cluster-sleep-interval" src="/gui/img/duk.png"></td>
                            <td><input class="queueOption" type="range" min="5" max="1000" value="500" class="slider" id="human-cluster-interval" name="human-cluster-interval" onchange="updateNumberField(this);"/></td>
                            <td><input type="number" id="human-cluster-interval-text" onchange="updateSlider(this);"></td>
                        </tr>
                        <tr>
                            <td><p>Tasks per Cluster</p></td>
                            <td class="duk-icon duk-table-icon"><img id="duk-task-per-cluster" src="/gui/img/duk.png"></td>
                            <td><input class="queueOption" type="range" min="1" max="20" value="5" class="slider" id="human-task-count" name="human-task-count" onchange="updateNumberField(this);"/></td>
                            <td><input type="number" id="human-task-count-text" onchange="updateSlider(this);"></td>
                        </tr>
                    </table>
                    <button id="generateLayer" type="button" class="button-success" style="" onclick="generateHuman();">Generate Human</button>
                    <div id="human-built">
                        <span id="message"></span>
                    </div>
                </div>
                <div class="column column-interior" style="text-align: left;">
                    <select id="existing-humans" style="width: 100%;margin-left: 0;" onchange="refreshSelectHuman(this);">
                        <option disabled="disabled" selected="">Select existing human...</option>
                        {% for h in humans %}
                        <option id="human-{{h.name}}" value="human-{{h.name}}">{{h.name}}</option>
                        {% endfor %}
                    </select>
                    <hr/>
                    <code id="delivery-commands" style="text-align: left; font-size:14px;"></code>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    $( document ).ready(function () {
        ['human-task-interval', 'human-cluster-interval', 'human-task-count'].forEach(elem => defaultNumberField(elem));
        stream('Design and generate a human to create noise on hosts');
     });
     $('#duk-task-sleep-interval').click(function() {
		 stream('Task Sleep Interval - Maximum sleep time between individual tasks executing')
	 });
     $('#duk-cluster-sleep-interval').click(function () {
     	stream('Task Cluster Sleep Interval - Maximum sleep time between clusters of tasks executing')
     });
     $('#duk-task-per-cluster').click(function () {
        stream('Tasks per Cluster - Maximum number of tasks to execute per cluster')
	 });
    $("#append-command").click(function () {
        $('#input-command').append('<div class="command-wrapper" style="display:flex"><input type="text" class="command" style="flex:90%" placeholder="Enter a single command to run"><div class="delete-command" onclick="deleteHumanCommand(this)">&times;</div><br class="command-break"></div>');
        stream('Input Custom Human Command');
    });
    $("#clear-commands").click(function () {
        $('.command-wrapper').remove();
        stream('Cleared Custom Human Commands');
    });
    function defaultNumberField(elem) {
        document.getElementById(elem+'-text').value = document.getElementById(elem).value;
    }
    function updateNumberField(elem){
        document.getElementById(elem.name+'-text').value=elem.value;
    }
    function updateSlider(elem) {
        let slider = elem.id.replace('-text', '');
        document.getElementById(slider).value = elem.value;
    }
    function generateHuman() {
        let cmdBlock = $("#delivery-commands");
        cmdBlock.empty();
        let humanName = $('#human-name').val();
        if (humanName === '') {
            warn('Enter a human name');
            return;
        }
        let platform = $("#base-platform").val();
        if (platform === null) {
            warn('Select a platform!');
            return;
        }
        let taskList = [];
        $('#human-tasks').find('li > input').each(function (e) {
            if ($(this).is(':checked')) {
                taskList.push($(this).attr('name').replace('human-', ''));
            }
        });
        if (taskList.length === 0) {
            warn('Select tasks for your human to execute!');
            return
        }
        let commands = []
        $('.command').each(function(){
            commands.push($(this).val())
        })
        let postData = {
            'index': 'build_human',
            'platform': platform,
            'name': humanName,
            'task_cluster_interval': $('#human-cluster-interval').val(),
            'task_interval': $('#human-task-interval').val(),
            'task_count': $('#human-task-count').val(),
            'tasks': taskList,
            'extra': commands
        };
        restRequest('POST', postData, renderBuiltHumanCallback, endpoint = '/plugin/human/api');
    }

    function deleteHumanCommand(elem) {
        $(elem).parent().remove();
    }

    function renderCommandBlock(taskClusterInterval, taskInterval, taskCount, extra, humanName, platform){
        let http = location.protocol+'//'+location.hostname+(location.port ? ':'+location.port: '');
        let baseHuman = "";
        let cmdBlock = $("#delivery-commands");
        cmdBlock.empty();
        $('#human-name-header').text(humanName);
        extra = renderExtra(extra, platform);
        switch (platform) {
            case "darwin":
                baseHuman = 'curl -sk -o '+humanName+'.tar.gz -X POST -H \'file:'+humanName+'.tar.gz\' '+http+'/file/download 2>&1 && mkdir '+humanName+' && tar -C '+humanName+' -zxvf '+humanName+'.tar.gz ' +
                    '&& cd '+humanName+' && virtualenv -p python3 '+humanName+' && '+humanName+'/bin/pip install -r requirements.txt && '+humanName+'/bin/python human.py --clustersize '+taskCount+' ' +
                    '--taskinterval '+taskInterval+' --taskgroupinterval '+taskClusterInterval+' --extra '+extra;
                break;
            case "linux":
                baseHuman = 'curl -sk -o '+humanName+'.tar.gz -X POST -H \'file:'+humanName+'.tar.gz\' '+http+'/file/download 2>&1 && mkdir '+humanName+' && tar -C '+humanName+' -zxvf '+humanName+'.tar.gz ' +
                    '&& cd '+humanName+' && virtualenv -p python3 '+humanName+' && '+humanName+'/bin/pip install -r requirements.txt && '+humanName+'/bin/python human.py --clustersize '+taskCount+' ' +
                    '--taskinterval '+taskInterval+' --taskgroupinterval '+taskClusterInterval+' --extra '+extra;
                break;
            case "windows-psh":
                baseHuman = '$server="'+http+'"; $url="$server/file/download"; $wc=New-Object System.Net.WebClient; $wc.Headers.add("file","'+humanName+'.zip"); $wc.DownloadFile($url, "$pwd\\'+humanName+'.zip"); ' +
                    'Expand-Archive '+humanName+'.zip -DestinationPath '+humanName+' -Force; cd '+humanName+'; python.exe -m venv '+humanName+';' +
                    '.\\'+humanName+'\\Scripts\\pip.exe install -r requirements.txt; .\\'+humanName+'\\Scripts\\python.exe human.py --clustersize '+taskCount+' ' +
                    '--taskinterval '+taskInterval+' --taskgroupinterval '+taskClusterInterval+' --extra '+extra;
                break;
        }
        cmdBlock.append(baseHuman);
    }

    function refreshSelectHuman(selected){
        restRequest('POST', {'index':'load_human', 'name':selected.value.replace('human-', '')}, renderLoadedHumanCallback, endpoint='/plugin/human/api');
    }

    function renderBuiltHumanCallback(data){
        if(data){
            let opts = $('#existing-humans');
            opts.append('<option id="human-'+data.name+'" value="human-'+data.name+'">'+data.name+'</option>');
            opts.find('option[value=human-'+data.name+']').attr('selected','selected');
            renderCommandBlock(data.task_cluster_interval, data.task_interval, data.tasks_per_cluster, data.extra, data.name, data.platform);
        }
    }

    function renderLoadedHumanCallback(data){
        if(data){
            renderCommandBlock(data[0].task_cluster_interval, data[0].task_interval, data[0].tasks_per_cluster, data[0].extra, data[0].name, data[0].platform);
        }
    }

    function renderExtra(extra, platform) {
        let formatted_commands = '';
        $.each(extra, function(i, command) {
            switch (platform) {
                case "darwin":
                    command = command.replace(/\\/g, '\\\\');
                    command = command.replace(/"/g, '\\\"');
                    break;
                case "linux":
                    command = command.replace(/\\/g, '\\\\');
                    command = command.replace(/"/g, '\\\"');
                    break;
            }
            formatted_commands += '"' + command + '" ';
        });
        return formatted_commands;
    }

</script>